<?php namespace Phpcmf\Controllers\Member;

class Son extends \Phpcmf\Table
{

    public $cid; // 内容id
    public $form; // 表单信息

    public function __construct() {
        parent::__construct();
        // 初始化模块
        $this->_module_init(APP_DIR);
        // 判断表单是否操作
        $this->form = $this->module['form'][\Phpcmf\Service::L('Router')->class];
        !$this->form && $this->_msg(0, dr_lang('模块表单【%s】不存在', \Phpcmf\Service::L('Router')->class));
        // 支持附表存储
        $this->is_data = 1;
        // 模板前缀(避免混淆)
        $this->tpl_prefix = '';
        // 单独模板命名
        $this->tpl_name = 'son';
        // 模块显示名称
        $this->name = dr_lang('内容模块[%s]表单（%s）', APP_DIR, $this->form['name']);
        // 获取父级内容
        $this->cid = intval(\Phpcmf\Service::L('Input')->get('cid'));
        if ($this->cid) {
            $this->index = $this->content_model->get_row( $this->cid);
            //$this->index['uid'] != $this->uid && $this->_msg(0, dr_lang('无权限操作他人数据'));
        }
        // 自定义条件
        $where = 'cid='. $this->cid;
        $sysfield = ['displayorder'];
        // 初始化数据表
        $this->_init([
            'field' => $this->form['field'],
            'table' => dr_module_table_prefix(APP_DIR).'_form_'.$this->form['table'],
            'sys_field' => $sysfield,
            'date_field' => 'inputtime',
            'list_field' => $this->form['setting']['list_field'],
            'show_field' => 'title',
            'order_by' => 'inputtime desc',
            'where_list' => $where,
        ]);
        // 写入模板
        \Phpcmf\Service::V()->assign([
            'mform' => $this->form,
            'index' => $this->index,
            'field' => $this->form['field'],
        ]);
    }

    public function index() {

        $tid = dr_safe_filename($_GET['tid']);
        $user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
        if($admin) {
            
        } elseif ($this->index['uid'] != $this->uid) {
			$this->_msg(0, dr_lang('这个不是您的圈子，无法修改分类'));
		}
        if (strlen($tid)) {
            $tid = intval($tid);
            $this->init['where_list'].= ' and tid='.$tid;
        }
        $this->init['order_by'] = 'displayorder asc,id asc';

        $p = [
            'cid' =>  $this->cid,
            'tid' =>  $tid,
        ];

        $this->_List($p);

        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);

        \Phpcmf\Service::V()->assign([
            'p' => $p,
            'stype' => $stype,
            'sname' => strlen($tid) ? $stype[$tid] : '全部',
        ]);
        \Phpcmf\Service::V()->display('son_list.html');
    }
    //实名发布弹窗接口
	public function shiming() {
	    $data = [
	        'url'=> dr_member_url('certify/home/index'),
	        'zhuangtai'=> 1
	        ];
	    $this->_json(1, dr_lang('不需实名'),$data);
    }
    //实名回复弹窗接口
	public function huifu() {
	    $data = [
	        'url'=> dr_member_url('certify/home/index'),
	        'zhuangtai'=> 1
	        ];
	    $this->_json(1, dr_lang('不需实名'),$data);
    }
    public function add() {

        !$this->cid && $this->_msg(0, dr_lang('缺少cid参数'));
		$leixing = \Phpcmf\Service::L('Input')->get('leixing');
		//发帖权限
		$fabu = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $this->cid)->get()->getRowArray();
		if ($this->index['uid'] == $this->member['id'] || d_guanlizuming($this->cid,$this->uid)) {
			//$this->index['uid'] == $this->member['id'];
		} else {
			if ($fabu['fabu'] != 1) {
				$this->_msg(0, dr_lang('没有开通发帖'));
			}
		}
		//判断字节
		$day_posts = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', 'son')->get()->getRowArray();
		$day_post = dr_string2array($day_posts['setting']);
		if (IS_AJAX_POST) {
		    
			//$datasc = intval(\Phpcmf\Service::L('Input')->post('csrf_test_name'));
			$datas = \Phpcmf\Service::L('Input')->post('data');
			if ($datas['tid'] === 0) {
			    $this->_msg(0, '请选择分类', '');
			}
			if ( !$datas['neirong']) {
				
				$this->_msg(0, '内容不能为空', '');
			}
			preg_match_all("/./us", dr_preg_html($datas['neirong']),$match);
			$shuliang = count($match[0]);
			$shaozijie_post = $day_post['shaozijie'];
			$duozijie_post = $day_post['duozijie'];
			if ($shuliang < $shaozijie_post) {
				$this->_msg(0, '不能少于'.$shaozijie_post.'个文字', '');
			}
			if ($shuliang > $duozijie_post) {
				$this->_msg(0, '不能多于'.$duozijie_post.'个文字', '');
			}
		}


        // 是否有验证码
        $this->is_post_code = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'code', $this->member);
        list($tpl) = $this->_Post(0);

        \Phpcmf\Service::V()->assign([
            'form' =>  dr_form_hidden(),
            'is_post_code' => $this->is_post_code,
        ]);
        if($leixing){
        	\Phpcmf\Service::V()->display('post-'.$leixing.'.html');
        } else {
        	\Phpcmf\Service::V()->display($tpl);
        }

    }

    public function edit() {

        !$this->cid && $this->_msg(0, dr_lang('缺少cid参数'));

        $id = intval(\Phpcmf\Service::L('Input')->get('id'));
		$leixing = \Phpcmf\Service::L('Input')->get('leixing');
		$xigai = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $id)->get()->getRowArray();
		

        // 是否有验证码
        $this->is_post_code = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'code', $this->member);
        
        list($tpl, $data) = $this->_Post($id);
		
		//dr_debug('uid', $xigai['uid'].$this->uid);
		//$row = \Phpcmf\Service::M()->db->table('module_form')->where('module', 'quan')->where('table', 'son')->get()->getRowArray();
		//$row = dr_string2array($row['setting']);
		//dr_debug('uid', $this->index['xiugai']);
		//var_dump($this->index);
		if ($this->index['xiugai'] == 0) {
			if ($this->index['uid'] == $this->uid || d_guanlizuming($this->cid,$this->uid)) {
				// 添加二次开发
			} else {
				$this->_msg(0, dr_lang('已经关闭修改'));
			}
		} else {
			if ($xigai['uid'] == $this->uid) {
				// 添加二次开发
			} else {
				if ($this->index['uid'] == $this->uid) {
					//// 添加二次开发
				} else {
					$this->_msg(0, dr_lang('这个不是您的帖子，无法修改'));
				}
				
			}
		}
		
		

        !$data && $this->_msg(0, dr_lang('数据不存在: '.$id));
        $this->cid != $data['cid'] && $this->_msg(0, dr_lang('cid不匹配'));

        \Phpcmf\Service::V()->assign([
            'form' =>  dr_form_hidden(),
            'is_post_code' => $this->is_post_code,
			'data'=>$data,
        ]);
        if($leixing){
        	\Phpcmf\Service::V()->display('post-'.$leixing.'.html');
        } else {
        	\Phpcmf\Service::V()->display($tpl);
        }
    }

    public function order_edit() {
        $this->_Display_Order(
            intval(\Phpcmf\Service::L('Input')->get('id')),
            intval(\Phpcmf\Service::L('Input')->get('value'))
        );
    }

    public function del() {
        $this->_Del(
            \Phpcmf\Service::L('Input')->get_post_ids(),
            null,
            function ($rows) {
                // 对应删除提醒
                foreach ($rows as $t) {
                    \Phpcmf\Service::M('member')->delete_admin_notice(MOD_DIR.'/'.$this->form['table'].'_verify/edit:cid/'.$t['cid'].'/id/'.$t['id'], SITE_ID);// clear
                    \Phpcmf\Service::L('cache')->clear('module_'.MOD_DIR.'_from_'.$this->form['table'].'_show_id_'.$t['id']);
                }
            },
            \Phpcmf\Service::M()->dbprefix($this->init['table'])
        );
    }
    public function dels() {
		//同时删除评论
		$ids=\Phpcmf\Service::L('Input')->get_post_ids();
		\Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_'.$this->form['table'].'_comment')->where('cid', $ids)->delete();
		\Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_'.$this->form['table'].'_comment_index')->where('cid', $ids)->delete();
	    $this->_Del(
		    
	        \Phpcmf\Service::L('Input')->get_post_ids(),
	        null,
	        function ($rows) {
	            // 对应删除提醒
	            foreach ($rows as $t) {
	                \Phpcmf\Service::M('member')->delete_admin_notice(MOD_DIR.'/'.$this->form['table'].'_verify/edit:cid/'.$t['cid'].'/id/'.$t['id'], SITE_ID);// clear
	                \Phpcmf\Service::L('cache')->clear('module_'.MOD_DIR.'_from_'.$this->form['table'].'_show_id_'.$t['id']);
	            }
	        },
	        \Phpcmf\Service::M()->dbprefix($this->init['table'])
	    );
	}
	//奖励
	public function jiangli() {
		
			$quan = $_GET['quan'];
			$qiandao = 1;
			//奖励
			//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->module['name'])->get()->getRowArray();
			//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->form['table'])->get()->getRowArray();
			//$jiangli = dr_string2array($jianglis['setting']);
			//新的奖励制度
			$jianglis = \Phpcmf\Service::M()->db->table('1_quan_shezhi')->where('id', 4)->get()->getRowArray();
			$jianglidata = dr_string2array($jianglis['setting']);
			$jiangli = $jianglidata['pages'];
			
			$yuedujiangli = [
				'jingyan' => intval($jiangli['quan_jingyan_jiangli']),
				'jifen' => intval($jiangli['quan_jifen_jiangli']),
				'yue' => intval($jiangli['quan_jifen_rmb']),
				'cishu' => $cishu,
				'miaoshu' => intval($jiangli['quan_jifen_jiangli_shijian']),
			];
			$urls = '/index.php?s='.MOD_DIR.'&c=son&id='.$quan;
			
			// 增加经验
			\Phpcmf\Service::M('member')->add_experience($this->uid, $yuedujiangli['jingyan'], dr_lang('阅读奖励').$yuedujiangli['jingyan'].SITE_EXPERIENCE, $url = $urls, $mark = $quan, $count = 1, $tieid = $quan);
			// 增加积分
			\Phpcmf\Service::M('member')->add_score($this->uid, $yuedujiangli['jifen'], dr_lang('阅读奖励').$yuedujiangli['jifen'].SITE_SCORE, $url = $urls, $mark = $quan, $count = 1, $tieid = $quan);
			// 增加作者积分
			\Phpcmf\Service::M('member')->add_score($quandata['uid'], $yuedujiangli['jifen'], dr_lang('您的文章被阅读奖励了').$yuedujiangli['jifen'].SITE_SCORE, $url = $urls, $mark = $quan, $count = 1, $tieid = $quan);
		$this->_json(0, dr_lang('阅读奖励'), $qiandao);
		if (IS_AJAX) {}
		$this->_json(0, dr_lang('请求错误'));
	}


    // 隐藏或者启用
    public function vip_edit() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
        !$row && $this->_json(0, dr_lang('数据不存在'));

        $v = $row['vip'] ? 0 : 1;
        \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['vip' => $v]);

        exit($this->_json(1, dr_lang($v ? '已设置收费' : '已设置免费'), ['value' => $v]));
    }

    // 分类操作
    public function son_add() {

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
            if (!$name) {
                $this->_json(0, dr_lang('名称没有填写'));
            }
			$name = [
				'id' => '',
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
			];
            $rt = \Phpcmf\Service::M('content', 'quan')->add_son_type($this->index, $name);
            if (!$rt['code']) {
                $this->_json(0, $rt['msg']);
            }
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => '',
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;
    }

    public function son_edit() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
            !$name && $this->_json(0, dr_lang('名称没有填写'));
			$stype[$tid] = [
				'id' => $tid,
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
			];
			//$stype[$quanxian] = $quanxian;
            \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($this->cid, [
                'son_type' => dr_array2string($stype)
            ]);
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => $stype[$tid],
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;

    }

    public function son_del() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);
        !$stype[$tid] && $this->_json(0, dr_lang('分类不存在'));
        unset($stype[$tid]);
        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->where('uid', $this->uid)->update($this->cid, [
            'son_type' => dr_array2string($stype)
        ]);

        /*$data = \Phpcmf\Service::M()->table($this->init['table'])->where('uid', $this->uid)->where('cid', $this->cid)->where('tid', $tid)->getAll();
        if ($data) {
            foreach ($data as $t) {
                $_POST['ids'][] = $t['id'];
            }
            $this->del();
        }*/

        $this->_json(1, dr_lang('操作成功'));

    }
	// 添加bug
	public function son_add_bug() {
		/*if ($this->index['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限添加'.$this->index['uid']));
		}*/
		
		$quanid = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $_GET['indexid'])->get()->getRowArray();
		if ($quanid['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限添加'.$quanid['uid']));
		}
	
	    if (IS_POST) {
	        $name = dr_safe_replace($_POST['name']);
			$zhishixian = dr_safe_replace($_POST['zhishixian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
	        if (!$name) {
	            $this->_json(0, dr_lang('名称没有填写'));
	        }
			 
			
			$name = [
				'id' => '',
				'name' => $name,
				'zhishixian' => $zhishixian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
				'shijian' => SYS_TIME,
				'uid' => $this->uid,
			];
			$bugshuju = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $_GET['id'])->get()->getRowArray();
			
	        $rt = \Phpcmf\Service::M('content', MOD_DIR)->add_son_bug($bugshuju, $name);
	        if (!$rt['code']) {
	            $this->_json(0, $rt['msg']);
	        }
	        $this->_json(1, dr_lang('操作成功'));
	    }
	    $value = [
			'name'=>'',
			'zhishixian'=>''
		];
	    \Phpcmf\Service::V()->assign([
	        'value' => $value,
	    ]);
	    \Phpcmf\Service::V()->display('son_bug.html');exit;
	}
	public function son_edit_bug() {
		$quanid = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $_GET['indexid'])->get()->getRowArray();
		if ($quanid['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限修改'.$quanid['uid']));
		}
	
	    $tid = intval($_GET['tid']);
		$bugshuju = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $_GET['id'])->get()->getRowArray();
	    $stype = \Phpcmf\Service::M('content', MOD_DIR)->son_bug($bugshuju['bugtitle']);
		
	
	    if (IS_POST) {
	        $name = dr_safe_replace($_POST['name']);
			$zhishixian = dr_safe_replace($_POST['zhishixian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
	        !$name && $this->_json(0, dr_lang('名称没有填写'));
			$stype[$tid] = [
				'id' => $tid,
				'name' => $name,
				'zhishixian' => $zhishixian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
				'shijian' => SYS_TIME,
				//'uid' => $this->uid,
			];
			//$stype[$zhishixian] = $zhishixian;
	        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->update($_GET['id'], [
	            'bugtitle' => dr_array2string($stype)
	        ]);
	        $this->_json(1, dr_lang('操作成功'));
	    }
	
	    \Phpcmf\Service::V()->assign([
	        'value' => $stype[$tid],
	    ]);
	    \Phpcmf\Service::V()->display('son_bug.html');exit;
	
	}
	public function son_del_bug() {
		$quanid = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $_GET['indexid'])->get()->getRowArray();
		if ($quanid['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限删除'.$quanid['uid']));
		}
	
	    $tid = intval($_GET['tid']);
		
		$bugshuju = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $_GET['id'])->get()->getRowArray();
		$stype = \Phpcmf\Service::M('content', MOD_DIR)->son_bug($bugshuju['bugtitle']);
	    !$stype[$tid] && $this->_json(0, dr_lang('分类不存在'));
	    unset($stype[$tid]);
	    \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->update($_GET['id'], [
	        'bugtitle' => dr_array2string($stype)
	    ]);
	
	    $this->_json(1, dr_lang('操作成功'));
	
	}
	// 一级分类操作
	public function son_add_yiji() {
		$cid = intval($_GET['cid']);
		/*$daid = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$cid)->getAll();
		$erjiids= max($daid);*/
		//dr_debug('max', $erjiids['id']);
		//$yijiid = intval($_GET['yijiid']);
		//判断是否是圈子的圈主
		$quanzhuid = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR))->where('id',$cid)->where('uid',$this->uid)->get()->getRowArray();
		// 导航数量
		/*$erjishuliang = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('cid', $cid)->where('yijiid', $yijiid)->countAllResults();*/
		$user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
		if (!$quanzhuid && !$admin) {
			$this->_json(1, dr_lang('您不是这个圈的圈主,无法添加分类'));
		}
		//$erjiid = $yijiid*100+$erjishuliang+1;
		if (IS_POST) {
			
			$name = dr_safe_replace($_POST['name']);
			$yincang = dr_safe_replace($_POST['yincang']);
			$fabu = dr_safe_replace($_POST['fabu']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
			\Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->insert(array(
			    'cid' => $cid,
			    'uid' => $this->uid,
				'yijiid' => 0,
				'tieid' => 0,
				'name' => $name,
				'yincang' => $yincang,
				'fabu' => $fabu,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
				'inputtime' => SYS_TIME,
			));
			
			$this->_json(1, dr_lang('操作成功'));
		}
		\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
	}
	//一级分类修改
	public function son_edit_yiji() {
		$id = intval($_GET['id']);
		$tieid = intval($_GET['tieid']);
		$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$id)->get()->getRowArray();
		$user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
		if(!$admin && !$this->uid) {
			$this->_json(0, dr_lang('抱歉不是你的圈子，无法修改分类'));
		}
		if (IS_POST) {
			$name = dr_safe_replace($_POST['name']);
			$yincang = dr_safe_replace($_POST['yincang']);
			$fabu = dr_safe_replace($_POST['fabu']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->update($id,[
			    'name' => $name,
			    'yincang' => $yincang,
			    'fabu' => $fabu,
			    'yanse' => $yanse,
			    'paixu' => $paixu,
			    'moren' => $moren,
			    //'inputtime' => SYS_TIME,
			]);
			$this->_json(1, dr_lang('修改操作成功'));
		}
		\Phpcmf\Service::V()->assign([
		    'value' => $stype,
		]);
		\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
	}
	//一级分类删除
	public function son_del_yiji() {
		//圈子id
		$id = intval($_GET['id']);
		$tieid = intval($_GET['tieid']);
		$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$id)->get()->getRowArray();
		$user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
		if(!$admin && !$this->uid) {
			$this->_json(0, dr_lang('抱歉不是你的圈子，无法修改分类'));
		}
		$erjifenlei = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('yijiid',$id)->getAll();
		if ($erjifenlei) {
			$this->_json(1, dr_lang('分类下面有二级分类，不能删除,先删除二级分类'));
		}
		$neirong = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('tid',$id)->getAll();
		if ($neirong) {
			$this->_json(1, dr_lang('分类下面有帖子，不能删除'));
		}

		\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$id)->where('uid',$this->uid)->delete();
		$this->_json(1, dr_lang('操作成功'));
	}
	// 二级分类添加
	public function son_add_erji() {
		$id = intval($_GET['id']);
		$cid = intval($_GET['cid']);
		$daid = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$cid)->getAll();
		$erjiids= max($daid);
		//dr_debug('max', $erjiids['id']);
		$yijiid = intval($_GET['yijiid']);
		//判断是否是圈子的圈主
		$quanzhuid = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR))->where('id',$cid)->get()->getRowArray();
		// 导航数量
		$erjishuliang = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('cid', $cid)->where('yijiid', $yijiid)->countAllResults();
		$user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
		if (!$admin && !$this->uid) {
			$this->_json(1, dr_lang('您不是这个圈的圈主,无法添加二级分类'));
		}
		$erjiid = $id*100+$erjishuliang+1;
		if (IS_POST) {
			
			$name = dr_safe_replace($_POST['name']);
			$yincang = dr_safe_replace($_POST['yincang']);
			$fabu = dr_safe_replace($_POST['fabu']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
			\Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->insert(array(
			    'cid' => $cid,
			    'uid' => $this->uid,
			    'yijiid' => $id,
			    'tieid' => $erjiid,
			    'name' => $name,
			    'yincang' => $yincang,
			    'fabu' => $fabu,
			    'yanse' => $yanse,
			    'paixu' => $paixu,
			    'moren' => $moren,
			    'inputtime' => SYS_TIME,
			));
			
			$this->_json(1, dr_lang('操作成功'));
		}
		\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
	}
	//二级分类修改
	public function son_edit_erji() {
		$id = intval($_GET['id']);
		$tieid = intval($_GET['tieid']);
		$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$id)->get()->getRowArray();
		$user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
		if (!$admin && !$this->uid){
			$this->_json(0, dr_lang('您不是这个圈的圈主,无法修改分类'));
		}
		if (IS_POST) {
			$name = dr_safe_replace($_POST['name']);
			$yincang = dr_safe_replace($_POST['yincang']);
			$fabu = dr_safe_replace($_POST['fabu']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->update($id,[
			    'name' => $name,
			    'yincang' => $yincang,
			    'fabu' => $fabu,
			    'yanse' => $yanse,
			    'paixu' => $paixu,
			    'moren' => $moren,
			]);
			$this->_json(1, dr_lang('修改操作成功'));
		}
		\Phpcmf\Service::V()->assign([
		    'value' => $stype,
		]);
		\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
	}
	//二级分类删除
	public function son_del_erji() {
		//圈子id
		$id = intval($_GET['id']);
		$tieid = intval($_GET['tieid']);
		$neirong = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('cid',$tieid)->where('tid',$id)->getAll();
		$user =dr_member_info($this->uid);
        $admin = $user['is_admin'];
		if ($neirong) {
			$this->_json(1, dr_lang('分类下面有帖子，不能删除'));
		}
		$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$id)->get()->getRowArray();
		if (!$admin && !$this->uid){
			$this->_json(0, dr_lang('您不是这个圈的圈主,无法删除分类'));
		}
		\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_fenlei')->where('id',$id)->delete();
		$this->_json(1, dr_lang('操作成功'));
	}
	// 迁移页面
	public function qianyi() {
		$id = intval($_GET['id']);
		           
		$neirong = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('id',$id)->get()->getRowArray();
		if (!$neirong) {
			//$this->_json(1, dr_lang('没有这个帖子'));
			return dr_return_data(0, dr_lang('没有这个帖子'));
		}
		\Phpcmf\Service::V()->display('son_qianyi.html');
	}
	// 迁移帖子
	public function qianyiid() {
		//圈子id
		$id = intval($_GET['id']);
		$cid = intval($_GET['cid']);
		$user =dr_member_info($this->uid);
		$admin = $user['is_admin'];
		if($admin){
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->update($id,[
			    'cid' => $cid,
				'tid' => 0,
			]);
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_data_0')->update($id,[
			    'cid' => $cid,
			]);
			$this->_json(1, dr_lang('迁移成功'));
		} else {
			$this->_json(1, dr_lang('不是管理员无法操作'));
		}
		
	}

    /**
     * 获取内容
     * $id      内容id,新增为0
     * */
    protected function _Data($id = 0) {

        $row = $this->content_model->get_form_row($id, $this->form['table']);
        if (!$row) {
            return [];
        }

        return $row;
    }

    // 格式化保存数据 保存之前
    protected function _Format_Data($id, $data, $old) {

        // 验证父数据
        !$this->index && $this->_json(0, dr_lang('关联内容不存在'));
		if (!$id) {
			if ($this->uid) {
				
				// 判断日发布量
				$day_posts = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', 'son')->get()->getRowArray();
				$day_post = dr_string2array($day_posts['setting']);
				 
				if ($day_post['day_post'] && \Phpcmf\Service::M()->db
						->table($this->init['table'])
						->where('uid', $this->uid)
						->where('DATEDIFF(from_unixtime(inputtime),now())=0')
						->countAllResults() >= $day_post['day_post']) {
					$this->_json(0, dr_lang('每天发布数量不能超过%s个', $day_post['day_post']));
				}
			
				// 判断发布总量
				if ($total_post['total_post'] && \Phpcmf\Service::M()->db
						->table($this->init['table'])
						->where('uid', $this->uid)
						->countAllResults() >= $total_post['total_post']) {
					$this->_json(0, dr_lang('发布数量不能超过%s个', $total_post['total_post']));
				}
				
			}
		}
		//发布之前钩子
		\Phpcmf\Hooks::trigger_callback('module_form_post_zhiqian', $data);

        // 默认数据
		// 判断老数据
		if ($old['uid']) {
			//哪位用户的数据 等于本人那么id为$this->member['uid']
			if ($this->$old['uid'] == (int)$this->member['uid']){
				$data[0]['uid'] = (int)$this->member['uid'];
				$data[1]['author'] = $old['author'];
			} else {
				$data[0]['uid'] = (int)$old['uid'];
				$data[1]['author'] = $old['author'];
			}
		} else {
			$data[0]['uid'] = $data[1]['uid'] = (int)$this->member['uid'];
			$data[1]['author'] = $this->member['username'] ? $this->member['username'] : 'guest';
		}
        //$data[0]['uid'] = $data[1]['uid'] = (int)$this->member['uid'];
        //$data[1]['author'] = $this->member['username'] ? $this->member['username'] : 'guest';
        $data[1]['cid'] = $data[0]['cid'] =  $this->cid;
        $data[1]['catid'] = $data[0]['catid'] = (int)$this->index['catid'];
		$data[1]['shenhe'] = (int)$this->index['shenhetiezi'];
        $data[1]['inputip'] = \Phpcmf\Service::L('Input')->ip_info();
		$data[1]['keywords'] = d_huati_zu_data_mform($data[1]['neirong']);
		//图集
		$data[1]['jgtkg'] = ($_GET['leixing'] === 'tuji') ? 1 : 0;
		//视频
		$data[1]['spkg'] = ($_GET['leixing'] === 'shipin') ? 1 : 0;
		//mp3
		$data[1]['mpsankaiguan'] = ($_GET['leixing'] === 'mpsan') ? 1 : 0;
		//语音
		$data[1]['yuyinkaiguan'] = ($_GET['leixing'] === 'yuyin') ? 1 : 0;
		//投票
		$data[1]['tpkg'] = ($_GET['leixing'] === 'toupiao') ? 1 : 0;
		//付费
		$data[1]['ffzt'] = ($_GET['leixing'] === 'fufei') ? 1 : 0;
		//问答
		$data[1]['wendakaiguan'] = ($_GET['leixing'] === 'wenda') ? 1 : 0;
		//商品
		$data[1]['spspkg'] = ($_GET['leixing'] === 'shangpin') ? 1 : 0;
		//比赛
		$data[1]['bisaikaiguan'] = ($_GET['leixing'] === 'bisai') ? 1 : 0;
		//下载
		$data[1]['xzzt'] = ($_GET['leixing'] === 'xiazai') ? 1 : 0;
		//PK
		$data[1]['pkkaiguan'] = ($_GET['leixing'] === 'pk') ? 1 : 0;
		//活动
		$data[1]['hdzt'] = ($_GET['leixing'] === 'huodong') ? 1 : 0;
		//出租
		$data[1]['chuzukaiguan'] = ($_GET['leixing'] === 'chuzu') ? 1 : 0;
		//求租
		$data[1]['qiuzukaiguan'] = ($_GET['leixing'] === 'qiuzu') ? 1 : 0;
		//招聘
		$data[1]['zpkg'] = ($_GET['leixing'] === 'zhaopin') ? 1 : 0;
		//求职
		$data[1]['qzkg'] = ($_GET['leixing'] === 'qiuzhi') ? 1 : 0;
		if(dr_is_app('chuangyejin')){
			//创业金
			$data[1]['fczt'] = ($_GET['leixing'] === 'chuangyejin') ? 1 : 0;
		}
        //$data[1]['inputtime'] = SYS_TIME;
		$m = \Phpcmf\Service::L('Input')->get('m');
		if ($m == 'edit') {
			$data[1]['updatetime'] = SYS_TIME;
		} else {
			$data[1]['inputtime'] = SYS_TIME;
		}

        // 审核状态
        $data[1]['status'] = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'verify', $this->member) ? 0 : 1;

        if (!$id) {
            $data[1]['tableid'] = 0;
            $data[1]['displayorder'] = 0;
        }

        return $data;
    }

    /**
     * 保存内容
     * $id      内容id,新增为0
     * $data    提交内容数组,留空为自动获取
     * $func    格式化提交的数据
     * */
    protected function _Save($id = 0, $data = [], $old = [], $func = null, $func2 = null) {

        return parent::_Save($id, $data, $old, null,
            function ($id, $data, $old) {
                //审核通知
                if ($data[1]['status']) {
                    // 增减金币
                    $score = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'score', $this->member);
                    $score && \Phpcmf\Service::M('member')->add_score($this->member['uid'], $score, dr_lang('%s: %s发布', MODULE_NAME, $this->form['name']), $this->index['curl']);
                    // 增减经验
                    $exp = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'exp', $this->member);
                    $exp && \Phpcmf\Service::M('member')->add_experience($this->member['uid'], $exp, dr_lang('%s: %s发布', MODULE_NAME, $this->form['name']), $this->index['curl']);
                    // clear
                    \Phpcmf\Service::L('cache')->clear('module_'.MOD_DIR.'_from_'.$this->form['table'].'_show_id_'.$id);
					if($_GET['m']=='add' && $data[1]['uid']==$this->uid){
					//更新帖子数量
					$tieshu = \Phpcmf\Service::M()->db->table('1_quan_form_son')->where('shenhe', 1)->where('cid', $this->index['id'])->countAllResults();;
					\Phpcmf\Service::M()->db->table('1_quan')->where('id', $this->index['id'])->update([
						'tieshu' =>$tieshu,
					]);
					
					//更新会员帖子数量
						$membertieshu = \Phpcmf\Service::M()->db->table('1_quan_form_son')->where('shenhe', 1)->where('uid', $this->uid)->countAllResults();;
						\Phpcmf\Service::M()->db->table('member')->where('id', $this->uid)->update([
							'tieshu' =>$membertieshu,
						]);
					}
                    // 保存之后的更新total字段
					//标题
					if($data[1]['title']){
					 $title = $data[1]['title'];
					} else {
					 $title = dr_strcut(dr_clearhtml($data[1]['neirong']), 20); //获取帖子内容
					}
					$zhuangtai = $data[1]['shenhe'];
					if($zhuangtai==1){
						$hedui = '发布成功';
					} else {
						$hedui = '待核对';
					}
					
                    $this->content_model->update_form_total( $this->cid, $this->form['table']);
					//发布通知给用户
					$dataim = [
						 'catid'=> 3, //类型
						 'title' => '系统:发布帖子通知', //标题
						 'zhiding' => '', //置顶
						 'content'=> '('.$hedui.'):'.$title, //通知内容
						 'uid'=> $this->uid, //发布人的uid
						 'faxinren'=> $this->uid, //发布人的uid
						 'shouxinren'=> $this->uid,//收信人
						 'tupian'=> '', //图片
						 'shipin'=> '', //视频
						 'yuyin'=> '', //语音
						 'fujian'=> '', //附件
						 'yidu'=> 0, //已读
						 'quanziid'=> $data[1]['cid'], //圈子ID
						 'quantieid'=> $data[1]['id'], //圈帖ID
						 'url' => '',
						];
						\Phpcmf\Service::M('auth')->addtongzhi($dataim);
						//发布通知给ID=1 的管理员
						if($this->uid == 1){
							
						} else {
							$dataim2 = [
							 'catid'=> 3, //类型
							 'title' => '系统:发布帖子通知', //标题
							 'zhiding' => '', //置顶
							 'content'=> '('.$hedui.'):'.$title, //通知内容
							 'uid'=> $this->uid, //发布人的uid
							 'faxinren'=> $this->uid, //发布人的uid
							 'shouxinren'=> '1',//收信人
							 'tupian'=> '', //图片
							 'shipin'=> '', //视频
							 'yuyin'=> '', //语音
							 'fujian'=> '', //附件
							 'yidu'=> 0, //已读
							 'quanziid'=> $data[1]['cid'], //圈子ID
							 'quantieid'=> $data[1]['id'], //圈帖ID
							 'url' => '',
							];
							\Phpcmf\Service::M('auth')->addtongzhi($dataim2);
						}
                } else {
                    \Phpcmf\Service::M('member')->admin_notice(SITE_ID, 'content', $this->member, dr_lang('%s: %s提交内容审核', MODULE_NAME, $this->form['name']), MOD_DIR.'/'.$this->form['table'].'_verify/edit:cid/'. $this->cid.'/id/'.$id, SITE_ID);
                }
            }
        );
    }
	/**
	 * 回调处理结果
	 * $data
	 * */
	protected function _Call_Post($data) {
		$url = WEB_DIR.'index.php?s='.$this->module['dirname'].'&c=son&id='.$data[1]['id'];
		if ($data[1]['status']) {
			return dr_return_data(1, dr_lang('操作成功'), ['url' => $url,'id'=>$data[1]['id'],'data'=>$data[1]]);
		} else {
			return dr_return_data(1, dr_lang('操作成功，等待管理员审核'), ['url' => $url,'id'=>$data[1]['id'],'data'=>$data[1]]);
		}
	}
}
